package com.mx.tool.form.window.zh;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.net.URLEncoder;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.charset.Charset;
import java.util.Date;

/**
 * @Author: 徐建鹏.
 * @create: 2021-11-10 14:10
 * @Description:
 */
public class TimeUtcWindow {


    private JPanel jPanel;
    private JTextArea contentArea;
    private JTextArea showArea;
    private JButton utcToStrButton;
    private JComboBox<String> typeBox;
    private JTextField formatText;
    private JButton clearButton;
    private JButton hhButton;
    private JButton strToUtcButton;
    private JComboBox<String> comboBox1;


    public TimeUtcWindow() {
        comboBox1.addActionListener(e -> {
            String currentTime = Convert.toStr(comboBox1.getSelectedItem());
            String contentText;
            String showText;
            switch (currentTime){
                case "当前标准时间(秒)":
                    contentText = DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss");
                    showText = Convert.toStr(DateUtil.parse(contentText,"yyyy-MM-dd HH:mm:ss").getTime()/1000);
                    break;
                case "当前标准时间(毫秒)":
                    contentText = DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss.SSS");
                    showText = Convert.toStr(DateUtil.parse(contentText,"yyyy-MM-dd HH:mm:ss.SSS").getTime());
                    break;
                case "当前Unix时间戳(秒)":
                    showText = DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss");
                    contentText = Convert.toStr(DateUtil.parse(showText,"yyyy-MM-dd HH:mm:ss").getTime()/1000);
                    break;
                case "当前Unix时间戳(毫秒)":
                    showText = DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss.SSS");
                    contentText = Convert.toStr(DateUtil.parse(showText,"yyyy-MM-dd HH:mm:ss.SSS").getTime());
                    break;
                default:
                    contentText = DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss");
                    showText = Convert.toStr(DateUtil.parse(contentText,"yyyy-MM-dd HH:mm:ss").getTime()/1000);
                    break;
            }
            contentArea.setText(contentText);
            showArea.setText(showText);
        });
        utcToStrButton.addActionListener(e -> {
            String content = contentArea.getText();
            if(StringUtils.isNotBlank(content)){
                Long contentLong = Convert.toLong(contentArea.getText());
                String format = StringUtils.defaultIfBlank(formatText.getText(),"yyyy-MM-dd HH:mm:ss");
                showArea.setText(DateUtil.format(new Date(contentLong), format));
            }
        });
        strToUtcButton.addActionListener(e -> {
            String content = contentArea.getText();
            if(StringUtils.isNotBlank(content)){
                String type = Convert.toStr(typeBox.getSelectedItem());
                String format = StringUtils.defaultIfBlank(formatText.getText(),"yyyy-MM-dd HH:mm:ss");
                long time = DateUtil.parse(content,format).getTime();
                String timeStr = Convert.toStr(time);
                if("秒".equals(type)){
                    timeStr = Convert.toStr(time/1000);
                }
                showArea.setText(timeStr);
            }
        });
        clearButton.addActionListener(e -> {
            contentArea.setText("");
            showArea.setText("");
        });
        hhButton.addActionListener(e -> {
            String content = contentArea.getText();
            String show = showArea.getText();
            contentArea.setText(show);
            showArea.setText(content);
        });
    }


    public JPanel getContent() {
        return jPanel;
    }
}
